﻿// ActionScript file
package application.view.components.UI{
	import caurina.transitions.*;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	public class HScrollContainer extends Sprite{
		
		protected var leftArrow:Sprite;
		protected var rightArrow:Sprite;
		private var container:Sprite;
		
		protected var maskBitmap:Bitmap
		protected var maskBitmapData:BitmapData;
		private var scrollDirection:String;
		private var changed:Boolean=false;
		/**public properties*/
		//skin�?上下左右箭头、背景轨迹和拖拽�?
		protected var _scrollBarArrow:Object;
		protected var _scrollBarThumb:Sprite;
		protected var _scrollBarTrack:Sprite;
		//是否显示scrollBar，有三个�?auto,off,on.
		protected var horizontalScrollPolicy:String="auto";
		//滚动速率
		protected var _scrollSpeed:Number;
		//滚动距离，不管方�?
		private var _scrollH:Number;
		//滚动矩形
		private var _scrollRect:Rectangle;
		private var _itemContainer:Object
		
/** 		----------------------------------
		public getter/setter
		-------------------------------- **/

		public function set scrollBarArrow(value:Object):void{
			_scrollBarArrow=value;
		}
		public function get scrollBarArrow():Object{
			return _scrollBarArrow;
		}
		public function set scrollRectangle(value:Rectangle):void{
			_scrollRect=value;
		}
		public function get scrollRectangle():Rectangle{
			return _scrollRect;
		}
		public function set scrollH(value:Number):void{
			_scrollH=value;
		}
		public function set itemContainer(value:Object):void{
			_itemContainer=value;
			buildContainer();
		}
/** 		---------------------------------
		constructor
	----------------------------------- */	
		public function HScrollContainer(itemContainer:Object=null){
			_itemContainer=itemContainer;
			init();
		}
		private function init():void {
			buildMaskShape();
			buildContainer();
			
			if (horizontalScrollPolicy == "on" || horizontalScrollPolicy == "auto"){
				buildHScrollBar();
			}else return;
		}
		//创建遮罩
		private function buildMaskShape():void{
			maskBitmapData=new BitmapData(_scrollRect.width,_scrollRect.height);
			maskBitmap=new Bitmap(maskBitmapData);
			addChild(maskBitmap);
			//maskBitmap.x&y =0;
			maskBitmapData.fillRect(_scrollRect,0x000000);
		}
		private function buildContainer():void{
			if(container != null){
			var container:Sprite=new _itemContainer;
			addChild(container);
			container.mask=maskBitmap;
			changed=true;
			}
		}
		//创建滚动�?
		private function buildHScrollBar():void {
			//判断竖滚动条时，依据总高�?totalY)
			
			if(container.width > _scrollRect.width){
				addHScrollBar();
			}
		}
		private function addHScrollBar():void{
			leftArrow=new _scrollBarArrow();
			addChild(leftArrow);
			leftArrow.addEventListener(MouseEvent.MOUSE_DOWN,onLeftMouseDown);
			
			rightArrow=new _scrollBarArrow()  ;
			addChild(rightArrow);
			rightArrow.rotation=180;
			rightArrow.addEventListener(MouseEvent.MOUSE_DOWN,onRightMouseDown);
			measureHScrollBar();
			updateHScrollBar();
		}
		
		private function updateHScrollBar():void{
			if (container.x == 0) {
				//可以添加设置其消失，并添加消失效�?
				leftArrow.visible=false;
			}else {
				leftArrow.visible=true;
			}
			if (container.x ==  _scrollRect.width- container.width) {
				leftArrow.visible=false;
			}else {
				leftArrow.visible=true;
			}
			if(changed==true){
				updateScrollBarThumb();
			}
		}
		
		protected function measureHScrollBar():void{
			
		}
		//更新/重绘滑块
		protected function updateScrollBarThumb():void{
		}
		
		//根据scrollH/scrollV来移动容器�?
		private function activeItem():void {
			if (changed == true) {
				//无需考虑固定速率问题
				if(scrollDirection== "right"){
					Tweener.addTween(container,{_x:container.x-_scrollH,time:_scrollH/_scrollSpeed,transition:"easeOutExpo",
												onUpdate:tweenStop,onComplete:tweenComplete});
				}
				if(scrollDirection == "left"){
					Tweener.addTween(container,{_x:container.x+_scrollH,time:_scrollH/_scrollSpeed,transition:"easeOutExpo",
												onUpdate:tweenStop,onComplete:tweenComplete});
				}
			}
			updateHScrollBar();
		}
		private function tweenStop():void{
			if(container.x>this.x || container.x < this.width-container.width){
				Tweener.removeTweens(container,"_x");
			}
		}
		private function tweenComplete():void{
			updateHScrollBar();
		}
		/**-----------------------------------------
		Event Handlers
		-------------------------------------------*/
		private function onLeftMouseDown(e:MouseEvent):void {
			scrollDirection="left";
			changed=true;
			activeItem();
		}
		private function onRightMouseDown(e:MouseEvent):void {
			scrollDirection="right";
			changed=true;
			activeItem();
		}
		}
	}
}